package com.situ.firmadmin.controller;


import com.situ.firmadmin.annotation.MyLog;
import com.situ.firmadmin.pojo.Dept;
import com.situ.firmadmin.pojo.Reim;
import com.situ.firmadmin.pojo.User;
import com.situ.firmadmin.pojo.query.ReimQuery;
import com.situ.firmadmin.pojo.vo.DeptReimVO;
import com.situ.firmadmin.pojo.vo.TypeCountVO;
import com.situ.firmadmin.pojo.vo.UserReimVO;
import com.situ.firmadmin.service.IDeptService;
import com.situ.firmadmin.service.IReimService;
import com.situ.firmadmin.service.IUserService;
import com.situ.firmadmin.util.PageResult;
import com.situ.firmadmin.util.Result;
import com.situ.firmadmin.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reim")
public class ReimController {
    @Autowired
    private IReimService reimService;
    
    @Autowired
    private IUserService userService;
    
    @Autowired
    private IDeptService deptService;
    
    public User getUser(){
        Map<String, Object> map1 = ThreadLocalUtil.get();
        Integer id = (Integer) map1.get("id");
        return userService.selectById(id);
    }

    @GetMapping("/list")
    public Result selectByPage(ReimQuery reimQuery){
        User user = getUser();
        PageResult pageResult=reimService.selectByPage(reimQuery,user);
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getCount());
        map.put("list",pageResult.getData());
        return Result.ok(map);
    }

    @GetMapping("/selectByUserId")
    public Result selectByUserId( ReimQuery reimQuery){
        User user=getUser();
        PageResult pageResult=reimService.selectByUserId(user,reimQuery);
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getCount());
        map.put("list",pageResult.getData());
        return Result.ok(map);
    }
    
    @GetMapping("/selectByAdminId")
    public Result selectByAdminId(ReimQuery reimQuery ){
        User user=userService.getUser();
        PageResult pageResult=reimService.selectByAdminId(reimQuery,user);
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getCount());
        map.put("list",pageResult.getData());
        return Result.ok(map);
    }
    
    @PostMapping("/add")
    public Result insert(@RequestBody Reim reim){
        User user=getUser();
        reim.setDepartId(user.getDepartmentId());
        reim.setUserId(user.getId());
        reimService.insert(reim);
        return Result.ok("申请成功！");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result updateDelete(@PathVariable Integer id){
        User user=getUser();
        reimService.updateDelete(id,user);
        return Result.ok("删除成功！");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result updateDelete(@PathVariable Integer[] ids){
        reimService.updateDeleteAll(ids);
        return Result.ok("删除成功！");
    }

    @DeleteMapping("/deleted/{id}")
    public Result deleteById(@PathVariable Integer id){
        reimService.deleteById(id);
        return Result.ok("撤销成功！");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        UserReimVO userReimVO=reimService.selectById(id);
        return Result.ok(userReimVO);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Reim reim){
        reimService.update(reim);
        return Result.ok("修改成功！");
    }
    
    @MyLog(module = "报销审批")
    @PutMapping("/updateAudit")
    public Result updateAudit(@RequestBody UserReimVO userReimVO){
        Reim reim=new Reim();
        reim.setId(userReimVO.getId());
        reim.setStatus(userReimVO.getStatus());
        reim.setAuditExplain(userReimVO.getAuditExplain());
        reimService.updateAudit(reim);
        return Result.ok("审批成功！");
    }
    
    @GetMapping("/selectReimCount")
    public Result selectReimCount(){
        List<Dept> deptList=deptService.selectAll();
        List<DeptReimVO> deptReimVOList=new ArrayList<>();
        for (Dept dept:deptList){
            List<DeptReimVO> oneDeptList=new ArrayList<>();
            for(int i=1;i<=12;i++){
                DeptReimVO deptReimVO=new DeptReimVO();
                deptReimVO.setAmount(0);
                deptReimVO.setMonth(i);
                deptReimVO.setDeptName(dept.getName());
                oneDeptList.add(deptReimVO);
            }
            List<DeptReimVO> list=reimService.selectByMonth(dept.getId());
            for (DeptReimVO oneDeptReimVO:oneDeptList){
                for (DeptReimVO deptReimVO:list){
                    if(deptReimVO.getMonth()==oneDeptReimVO.getMonth()){
                        oneDeptReimVO.setAmount(deptReimVO.getAmount());
                    }
                }
                deptReimVOList.add(oneDeptReimVO);
            }
        }
        return Result.ok(deptReimVOList);
    }

}
